package com.pawernode.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ配置类，用于定义交换机、队列和绑定关系
 */
@Configuration
@ConfigurationProperties(prefix = "my")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RabbitConfig {

    private String exchangeName;
    private String queueDelayName;

    /**
     * 创建自定义交换机
     *
     * @return CustomExchange 自定义的交换机实例
     */
    @Bean
    public CustomExchange customExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type", "direct"); // 放一个参数
        // CustomExchange(String name, String type, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new CustomExchange(exchangeName,"x-delayed-message",true,false,arguments);
    }

    /**
     * 创建队列
     *
     * @return Queue 队列实例
     */
    @Bean
    public Queue queue() {
        return QueueBuilder
                .durable(queueDelayName)
                .build();
    }

    /**
     * 创建绑定关系
     * 将队列绑定到交换机，并指定路由键
     *
     * @param customExchange 自定义交换机
     * @param queue 队列
     * @return Binding 绑定关系实例
     */
    @Bean
    public Binding bindingNormal(CustomExchange customExchange, Queue queue) {
        // 绑定，也要指定路由键，加noargs()方法
        return BindingBuilder.bind(queue).to(customExchange)
                .with("plugin").noargs();
    }
}
